<?php
namespace appin\cms\controller\admin;

use app\common\exception\AdminException;
use appin\cms\Admin;
use appin\cms\model\Config;


class Index extends Admin
{
    /**
     * 系统设置
     * @return \think\response\View
     */
    public function system()
    {
        return $this->render('',[
            'config' => Config::find()
        ]);
    }

    /**
     * 保存系统配置
     * @return \think\response\Json
     * @throws AdminException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function systemdata()
    {
        $data   = $this->request->param();
        $config = Config::find();
        if ( $config ) {
            $config->save($data);
            return json(['state' => 'success']);
        } else {
            $data['lang_id'] = session('admin.lang.id');
            $createRs = Config::create($data);
            if ( $createRs ) {
                return json(['state' => 'success']);
            }
            throw new AdminException('添加失败');
        }
    }
}